$(function () {
    $("#jqGrid").jqGrid({
        url: '../businesstravel/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true },
            { label: '附件id', name: 'fileId', index: 'file_id', width: 80 ,hidden:true},
            { label: '流程状态', name: 'pistatus', index: 'pistatus', width: 130 ,formatter:pistatus
            },
            { label: '姓名', name: 'userdesc', index: 'userdesc', width: 80 },

            { label: '出差地点', name: 'place', index: 'place', width: 80 },
			{ label: '开始时间', name: 'startDate', index: 'start_date', width: 90,formatter:"date",formatoptions:{srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i'}  },
			{ label: '结束时间', name: 'endDate', index: 'end_date', width: 90,formatter:"date",formatoptions:{srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i'} },
			{ label: '出差天数', name: 'days', index: 'days', width: 80 }, 			
			{ label: '出差事由', name: 'reason', index: 'reason', width: 120 },
			{ label: '流程指向', name: 'nextTo', index: 'next_to', width: 80,hidden:true },
            { label: '创建时间', name: 'cdate', index: 'cdate', width: 90,formatter:"date",formatoptions:{srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i'} },
            { label: '创建者', name: 'cusername', index: 'cusername', width: 80 },
			{ label: '提交日期', name: 'submitDate', index: 'submit_date', width: 80 ,hidden:true},
			{ label: '提交人', name: 'submitPerson', index: 'submit_person', width: 80 ,hidden:true},
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80 ,formatter:getDictInfo},
            { label: '审批/抄送人', name: 'nextUser', index: 'next_user', width: 90 },
            { label: '流程ID', name: 'piid', index: 'piid', width: 80 ,hidden:true}
        ],
		viewrecords: true,
        height: 360,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    vm.getDictList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable:false,
        dictList:{},
		title: null,
		businessTravel: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable = false;
			vm.title = "新增";
			vm.businessTravel = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
            {
                alert("数据已提交，不能修改");
                return ;
            }
			vm.showList = false;
            vm.showtable = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            vm.businessTravel.startDate=$("#startDate").val();
            vm.businessTravel.endDate=$("#endDate").val();
			var url = vm.businessTravel.id == null ? "../businesstravel/save" : "../businesstravel/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.businessTravel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
            var len=ids.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
                {
                    alert("数据已提交，不能删除");
                    return ;
                }
            }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../businesstravel/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../businesstravel/info/"+id, function(r){
                vm.businessTravel = r.businessTravel;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        submit: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid!=0&&piid!=null&&piid!=""){
                alert("数据已提交，不能重复提交！");
                return;
            }
            var url = "../businesstravel/submit";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        processInt: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var pistatus=rowData.pistatus;
            if(pistatus.indexOf('审批完成')>-1){
                alert("审批已完成，不能再审批")
                return;
            }else
            if(pistatus.indexOf('被拒绝')>-1){
                alert("审批被拒绝，不能再审批")
                return;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = false;
            vm.showtable2 = true;
            vm.title = "审批";

            vm.getInfo(id)
        },

        process: function (event) {
            var url = "../businesstravel/process";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.businessTravel),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reject: function (event) {
            var url = "../businesstravel/reject";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.businessTravel),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        popHistory: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid==0||piid==null||piid==""){
                alert("没有流程！");
                return;
            }
            utils.popHistory(piid);
            var page = $("#popHistoryGrid").jqGrid('getGridParam','page');
            $("#popHistoryGrid").jqGrid('setGridParam',{
                postData:{
                    'piid': piid,
                },
                page:page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "历史记录",
                area: ['725px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popHistory"),
				/*  btn: ['确定', '取消'],
				 btn1: function (index) {
				 layer.close(index);
				 }*/
            });
        },
        getDictList: function(){
            var params = {
                dicttype:"qjlb"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList = r.qjlb;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        popUser: function(str){
            popPage.popUserGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择审批人",
                area: ['750px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popUser"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popUserGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    //vm.businessTravel.idno=rowData.userId;
                    if(str=='user1'){
                        vm.businessTravel.user1=rowData.username;
                    }else if(str=='user2'){
                        vm.businessTravel.user2=rowData.username;
                    }else if(str=='user3'){
                        vm.businessTravel.user3=rowData.username;
                    }

                    layer.close(index);
                }
            });
        },
        queryfile:function(){
            var Id = getSelectedRow();
            if(Id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", Id);
            utils.openDialogView('上传附件',"../sys/upload.html?fileId="+rowData.fileId,"900px","500px",2);
        },

	}
});